# Replication archive for 
# Coppock, Alexander, Donald P. Green, and Ethan Porter. 2022. 
# Does Digital Advertising Affect Vote Choice? Evidence from a Randomized Field Experiment 
# Research & Politics. 

rm(list = ls())
# setwd() to replication archive

library(tidyverse)
library(estimatr)
library(texreg)

zip_code_level <- read_rds("CGP_2022_zip_code_level.rds")


fit_1 <-
  lm_robust(dem_two_party_share_2018 ~ Z,
            data = zip_code_level)

fit_2 <-
  lm_robust(
    dem_two_party_share_2018 ~ Z +
      dem_two_party_share_2016_nona +
      dem_two_party_share_2014_nona + vote_2014_missing +
      dem_two_party_share_2012_nona + vote_2012_missing,
    data = zip_code_level
  )

fit_3 <-
  lm_robust(dem_two_party_share_2018 ~ treatment,
            data = zip_code_level)

fit_4 <-
  lm_robust(
    dem_two_party_share_2018 ~ treatment +
      dem_two_party_share_2016_nona +
      dem_two_party_share_2014_nona + vote_2014_missing +
      dem_two_party_share_2012_nona + vote_2012_missing,
    data = zip_code_level
  )

# to check coefficient names
texreg(list(fit_1, fit_2, fit_3, fit_4),
       include.ci = FALSE,
       digits = 3)

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.names = c(
    "Intercept",
    "Any Treatment Video",
    "Missingness Indicator (2016)",
    "Two Party Vote Share (2014)",
    "Missingness Indicator (2014)",
    "Two Party Vote Share (2012)",
    "Missingness Indicator (2012)",
    "Treatment Video 1",
    "Treatment Video 2"
  ),
  digits = 4,
  stars = 0.05,
  custom.note = "%stars. HC2 robust standard errors are in parentheses.",
  reorder.coef = c(2, 8, 9, 3, 4, 5, 6, 7, 1),
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_share_all_zip.tex"
)

# vote margin data ---------------------------------------------------

fit_1 <-
  lm_robust(dem_two_party_vote_margin_2018 ~ Z,
            data = zip_code_level)

fit_2 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ Z +
      dem_two_party_vote_margin_2016_nona +
      dem_two_party_vote_margin_2014_nona + vote_2014_missing +
      dem_two_party_vote_margin_2012_nona + vote_2012_missing,
    data = zip_code_level
  )

fit_3 <-
  lm_robust(dem_two_party_vote_margin_2018 ~ treatment,
            data = zip_code_level)

fit_4 <-
  lm_robust(
    dem_two_party_vote_margin_2018 ~ treatment +
      dem_two_party_vote_margin_2016_nona +
      dem_two_party_vote_margin_2014_nona + vote_2014_missing +
      dem_two_party_vote_margin_2012_nona + vote_2012_missing,
    data = zip_code_level
  )

# to check coefficient names
texreg(list(fit_1, fit_2, fit_3, fit_4),
       include.ci = FALSE,
       digits = 3)

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.names = c(
    "Intercept",
    "Any Treatment Video",
    "Missingness Indicator (2016)",
    "Two Party Vote Share (2014)",
    "Missingness Indicator (2014)",
    "Two Party Vote Share (2012)",
    "Missingness Indicator (2012)",
    "Treatment Video 1",
    "Treatment Video 2"
  ),
  digits = 4,
  stars = 0.05,
  custom.note = "%stars. HC2 robust standard errors are in parentheses.",
  reorder.coef = c(2, 8, 9, 3, 4, 5, 6, 7, 1),
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_vote_margin_all_zip.tex"
)


# vote total data ---------------------------------------------------

fit_1 <-
  lm_robust(two_party_vote_total_2018 ~ Z,
            data = zip_code_level)

fit_2 <-
  lm_robust(
    two_party_vote_total_2018 ~ Z +
      two_party_vote_total_2016_nona +
      two_party_vote_total_2014_nona + vote_2014_missing +
      two_party_vote_total_2012_nona + vote_2012_missing,
    data = zip_code_level
  )

fit_3 <-
  lm_robust(two_party_vote_total_2018 ~ treatment,
            data = zip_code_level)

fit_4 <-
  lm_robust(
    two_party_vote_total_2018 ~ treatment +
      two_party_vote_total_2016_nona +
      two_party_vote_total_2014_nona + vote_2014_missing +
      two_party_vote_total_2012_nona + vote_2012_missing,
    data = zip_code_level
  )

# to check coefficient names
texreg(list(fit_1, fit_2, fit_3, fit_4),
       include.ci = FALSE,
       digits = 3)

texreg(
  list(fit_1, fit_2, fit_3, fit_4),
  custom.coef.names = c(
    "Intercept",
    "Any Treatment Video",
    "Missingness Indicator (2016)",
    "Two Party Vote Share (2014)",
    "Missingness Indicator (2014)",
    "Two Party Vote Share (2012)",
    "Missingness Indicator (2012)",
    "Treatment Video 1",
    "Treatment Video 2"
  ),
  digits = 4,
  stars = 0.05,
  custom.note = "%stars. HC2 robust standard errors are in parentheses.",
  reorder.coef = c(2, 8, 9, 3, 4, 5, 6, 7, 1),
  include.ci = FALSE,
  include.adjrs = FALSE,
  include.rmse = FALSE,
  dcolumn = TRUE,
  booktabs = TRUE,
  use.packages = FALSE,
  table = FALSE,
  # file = "../output_for_overleaf/florida_regression_vote_total_all_zip.tex"
)
